package com.example.mvvmtest.service;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.support.annotation.Nullable;

import com.example.mvvmtest.model.bean.UserBean;
import com.example.mvvmtest.utils.LogUtil;

import java.util.Random;

public class MyBindService extends Service {
    public interface OnServiceListener<T> {
        void onResult(T result);
    }

    public class MyBinder extends Binder {
        public MyBindService getService() {
            return MyBindService.this;
        }
    }

    private MyBinder binder = new MyBinder();
    private OnServiceListener<UserBean> serviceListener;

    @Override
    public void onCreate() {
        LogUtil.e(getClass().getSimpleName() + ":" + "onCreate");
        super.onCreate();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        LogUtil.e(getClass().getSimpleName() + ":" + "onStartCommand");
        return START_NOT_STICKY;
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        LogUtil.e(getClass().getSimpleName() + ":" + "onBind");
        return binder;
    }

    @Override
    public boolean onUnbind(Intent intent) {
        LogUtil.e(getClass().getSimpleName() + ":" + "onUnbind");
        return false;
    }

    @Override
    public void onDestroy() {
        LogUtil.e(getClass().getSimpleName() + ":" + "onDestroy");
        super.onDestroy();
    }

    public int getRandomNumber() {
        Random random = new Random();
        int result = random.nextInt(10) + 1;
        UserBean userBean = new UserBean("Tom", "Jerry" + result, false);
        serviceListener.onResult(userBean);
        return result;
    }

    public void setServiceListener(OnServiceListener<UserBean> serviceListener) {
        this.serviceListener = serviceListener;
    }

}
